//
//  DeviceSettingsVC.swift
//  MIFI
//
//  Created by 金嗓子卡卡 on 2019/4/21.
//  Copyright © 2019 NJMifi. All rights reserved.
//

import UIKit

class DeviceSettingsVC: RootVC {
    private let datas = [("Account Manager","AccountManagerVC"),
                         ("SIM/PIN Manager","SimPinManagerVC"),
//                         ("SD Card Manager","SDCardManagerVC"),
                         ("Reset",""),
                         ("Restart Device","")]
    
    private lazy var tableView: UITableView = {
        let tab = UITableView(frame: CGRect(x: 0, y: naviView!.bottom, width: screenWidth, height: screenHeight - naviView!.bottom), style: .grouped)
        tab.backgroundColor = whiteColor
        tab.rowHeight = 50
        tab.delegate = self
        tab.dataSource = self
        tab.separatorStyle = .none
        tab.isScrollEnabled = false
        return tab
    }()
    
    override func configView() {
        configNaviView(title: "Device Settings")
        view.addSubview(tableView)
    }
    
    private func restartMiFi() {
        net_restoreFactory()
    }
    
    private func resetDevice() {
        net_restartDevice()
    }
    
    private func showAlert(index:Int) {
        var title = "Reset"
        var content = "Do you want to reset the device?"
        var action = UIAlertAction(title: "OK", style: .default) { [weak self] (action) in
            
             self?.restartMiFi()
            
        }
        if index == 3{
            title = "Restart MiFi"
            content = "Do you want to restart MiFi?"
            action = UIAlertAction(title: "OK", style: .default, handler: {[weak self] (action) in
               self?.resetDevice()
            })
        }
        
        let alertVC = UIAlertController(title: title, message: content, preferredStyle: .alert)
        alertVC.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: nil))
        alertVC.addAction(action)
        present(alertVC, animated: true, completion: nil)
    }
}

extension DeviceSettingsVC : UITableViewDelegate, UITableViewDataSource{
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = UITableViewCell(style: .default, reuseIdentifier: "cell")
        cell.textLabel?.text = datas[indexPath.row].0
        cell.textLabel?.font = UIFont.systemFont(ofSize: 18)
        cell.textLabel?.textColor = textGray
        cell.selectionStyle = .none
        cell.accessoryType = .disclosureIndicator
        
        let line = UIView(frame: CGRect(x: 8, y: 50 - 0.5, width: screenWidth - 16, height: 0.5))
        line.backgroundColor = textGray
        cell.contentView.addSubview(line)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        guard indexPath.row < 2 else {
            showAlert(index: indexPath.row)
            return
        }
        gotoNewVC(vcname: datas[indexPath.row].1)
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        return nil
    }
    
    func tableView(_ tableView: UITableView, viewForFooterInSection section: Int) -> UIView? {
        return nil
    }
}
